package com.vdg.fps.services;

import com.vdg.R;
import com.vdg.fps.LoginActivity;
import com.vdg.fps.app.FPSManagerment;
import com.vdg.fps.config.FPSConfig;
import com.vdg.fps.data.FaceBookAccount;
import com.vdg.fps.listener.OnQueryListAccountListener;
import com.vdg.fps.listener.OnRequestListnener;
import com.vdg.fps.notifycation.ServiceNotification;

import android.app.Activity;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class FPSService extends android.app.Service {
	private ServiceNotification mServiceNotification;
	private FPSManagerment mFpsManagerment;
	private final IBinder mBinder = new LocalBinder();

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return mBinder;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		// TODO Auto-generated method stub
		mServiceNotification = ServiceNotification.getInstance(this);
		updateServiceNotification();
		mFpsManagerment = FPSManagerment.getInstace();
	}

	protected void updateServiceNotification() {
		Notification n = new Notification(R.drawable.ic_launcher, "S2Talk",
				System.currentTimeMillis());
		n.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;

		Intent notificationIntent = new Intent(this, LoginActivity.class);
		notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		n.contentIntent = PendingIntent.getActivity(this, 0,
				notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
		n.setLatestEventInfo(this, "s2talk", "s2talk running", n.contentIntent);
		mServiceNotification.showNotification(this,
				FPSConfig.SERVICE_NOTIFICATION, n);
	}

	public class LocalBinder extends Binder {
		public FPSService getService() {
			return FPSService.this;
		}
	}

	public synchronized void setUpAccount(Activity activity,
			FaceBookAccount account, final OnRequestListnener listener) {
		mFpsManagerment.setUpAccount(activity, account, listener);
	}

	public synchronized void queryListAccount(
			OnQueryListAccountListener listener, Context context) {
		mFpsManagerment.queryListAccount(listener, context);
	}

	public synchronized void fetchGroupsList(FaceBookAccount account,
			OnRequestListnener listener) {
		mFpsManagerment.fetchGroupsList(account, listener);
	}

	public synchronized void fetchMembersList(FaceBookAccount account,
			String groupID, OnRequestListnener listener) {
		mFpsManagerment.getMembersInGroup(account, groupID, listener);
	}

	public synchronized void fetchFriendsList(FaceBookAccount account,
			OnRequestListnener listener) {
		mFpsManagerment.fetchFriendsList(account, listener);
	}

	public synchronized FaceBookAccount getFacebookAccount(int index) {
		return mFpsManagerment.getFacebookAccount(index);
	}

	public synchronized void FetchLikedPages(FaceBookAccount account,
			OnRequestListnener listener) {
		mFpsManagerment.getLikes(account, listener);
	}

	public synchronized void FetchMyPages(FaceBookAccount account,
			OnRequestListnener listener) {
		mFpsManagerment.getMyPages(account, listener);
	}
}
